package com.hongshan.wallet.core.wallet

import android.arch.lifecycle.Observer
import android.view.View
import com.hongshan.wallet.BR
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BasePagerAdapter
import com.hongshan.wallet.base.MyActivity
import kotlinx.android.synthetic.main.pager_item_commission.view.*

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : zhangxx
 * @date     : 2019/3/28
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
class CommissionPagerAdapter(val act: MyActivity<*, *>, val type: Int) : BasePagerAdapter<CommissionPagerViewModel>() {

    val vms = Array(if (type == 4) 4 else 5) {
        newVm(it)
    }

    override fun getLayoutId(position: Int) = R.layout.pager_item_commission

    override fun getBrId(position: Int) = BR.vm

    override fun getViewModel(position: Int) = vms[position]

    private fun newVm(position: Int): CommissionPagerViewModel {
        val typeDetails = if (type == 4) {
            when (position) {
                0 -> null
                1 -> 5
                2 -> 6
                3 -> 4
                else -> null
            }
        } else {
            when (position) {
                0 -> null
                1 -> 1
                2 -> 2
                3 -> 3
                4 -> 4
                else -> null
            }
        }
        return CommissionPagerViewModel(MyApplication.applicationContext, type, typeDetails)
    }

    override fun getCount() = if (type == 4) 4 else 5

    override fun dispose(view: View, vm: CommissionPagerViewModel) {
        super.dispose(view, vm)
        vm.loadingComplete.observe(act, Observer {
            view.srlRecord.finishRefresh()
            view.srlRecord.finishLoadMore()
        })
    }

}